import 'package:flutter/material.dart';
import 'package:summer/navigator/hi_navigator.dart';
import 'package:uikit/http/core/hi_net_error.dart';
import 'package:uikit/http/dao/login_dao.dart';
import 'package:uikit/utils/toast_util.dart';

import 'login_base_view_model.dart';

class RegisterViewModel extends LoginBaseViewModel {
  @override
  void sendUserRequest(String userName, String password, {String? imoocId, String? orderId}) {
    sendRegisterRequest(userName, password, imoocId!, orderId!);
  }

  /// 发送注册请求
  void sendRegisterRequest(
    String userName,
    String password,
    String imoocId,
    String orderId,
  ) async {
    try {
      var result = await LoginDao.register(userName, password, imoocId, orderId);
      print("sendRegisterRequest result = $result");
      if (result['code'] == 0) {
        print('注册成功');
        ToastUtil.show("注册成功");
        HiNavigator.getInstance().onJumpTo(RouteStatus.login);

        //showToast('注册成功');
        // if (widget.onJumpToLogin != null) {
        //   widget.onJumpToLogin();
        // }
      } else {
        print(result['msg']);
        //showWarnToast(result['msg']);
      }
    } on NeedAuth catch (e) {
      ToastUtil.show(e.message);
      print("sendRegisterRequest NeedAuth = $e");
      Navigator.of(context!).pushNamed("/Login");
      //showWarnToast(e.message);
    } on HiNetError catch (e) {
      ToastUtil.show(e.message);
      print("sendRegisterRequest HiNetError = $e");
      //showWarnToast(e.message);
    }
  }
}
